package common.dao.Impl;

import cn.hutool.core.util.StrUtil;
import common.dao.UserRoleDao;
import common.domain.UserRole;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UserRoleDaoImpl implements UserRoleDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(UserRole userRole) {
        String sql = "insert into user_role\n" +
                "        (user_id,role_id) \n" +
                "values(?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,userRole.getUserId());
            psmt.setLong(2,userRole.getRoleId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(UserRole userRole) {
        String sql = "update user_role set";
        List<Object> param = new ArrayList<>();
        if (userRole == null){
            return;
        }else{
            if(userRole.getId() != null){
                sql += " id = ?,";
                param.add(userRole.getId());
            }
            if(userRole.getUserId() != null){
                sql += " user_id = ?,";
                param.add(userRole.getUserId());
            }
            if(userRole.getRoleId() != null){
                sql += " role_id = ?,";
                param.add(userRole.getRoleId());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,userRole.getId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from user_role where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public UserRole selectById(Long id) {
        String sql = "select * from user_role where id = ?";
        UserRole userRole = new UserRole();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                userRole.setId(rs.getLong(1));
                userRole.setUserId(rs.getLong(2));
                userRole.setRoleId(rs.getLong(3));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return userRole;
    }

    @Override
    public List<UserRole> selectList(UserRole userRole) {
        String sql = "select * from user_role where 1=1";
        List<UserRole> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (userRole == null){
            sql = "select * from user_role";
        }else{
            if(userRole.getId() != null){
                sql += " and id = ?";
                param.add(userRole.getId());
            }
            if(userRole.getUserId() != null){
                sql += " and user_id = ?";
                param.add(userRole.getUserId());
            }
            if(userRole.getRoleId() != null){
                sql += " and role_id = ?";
                param.add(userRole.getRoleId());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                UserRole userRole1 = new UserRole();
                userRole1.setId(rs.getLong(1));
                userRole1.setUserId(rs.getLong(2));
                userRole1.setRoleId(rs.getLong(3));
                list.add(userRole1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
